﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Googler.DAL;
using System.Data;
using HodhodNews.Storage;
using Googler.BLL;

namespace HodhodNews.Storage
{
    public class SqlServerStorageProvider : StorageProvider
    {
        const string SP_AddNews = "News_Add";
        const string SP_UpdateContent = "News_UpdateContent";
        const string SP_GetNewsById = "News_GetById";
        const string SP_GetAllNews = "News_GetAll";
        const string SP_GetToday = "News_GetTodayNews";
        const string SP_GetNewsBetween = "News_GetNewsBetween";

        const string _OId = "@OId";
        const string _EntryId = "@EntryId";
        const string _Title = "@Title";
        const string _Updated = "@Updated";
        const string _Link = "@Link";
        const string _ContentSummery = "@ContentSummery";
        const string _ContentText = "@ContentText";
        const string _Source = "@Source";
        const string _IsFullText = "@IsFullText";
        const string _Date = "@Date";
        const string _FromDate = "@fromDate";
        const string _ToDate = "@toDate";
        const string _LanguageID = "@LanguageID";



        /// <summary>
        /// Save current News List to database
        /// </summary>
        public override void Save(List<News> list)
        {
            foreach (News atom in list)
            {
                this.Create(atom);
            }
        }

        public override bool Create(News entity)
        {

            IDataAcessLayerBaseInterface DAL = DALHelper.GetDataAccessLayer();
            DAL.OpenConnection();
            DAL.ExecuteNonQuery(SP_AddNews,
                _OId, entity.OId,
                _EntryId, entity.EntryId,
                _Title, entity.Title,
                _Updated, entity.ModificationDate,
                _Link, entity.Link,
                _ContentSummery, entity.SummaryHtml,
                _Source, entity.Publisher,
                _LanguageID, entity.LanguageID
               );
            DAL.CloseConnection();
            return false;
        }
        public override void Update(News entity)
        {
            IDataAcessLayerBaseInterface DAL = DALHelper.GetDataAccessLayer();
            DAL.OpenConnection();
            DAL.ExecuteNonQuery(SP_UpdateContent,
                _OId, entity.OId,
                _ContentText, entity.SummaryHtml
               );
            DAL.CloseConnection();

        }
        private void TransformToAtomEntity(News entity, IDataReader dr)
        {
            entity.OId = DBConverter.ToInt32(dr["OId"]);
            entity.EntryId = DBConverter.ToString(dr["EntryId"]);
            entity.Title = DBConverter.ToString(dr["Title"]);
            entity.ModificationDate = DBConverter.ToDateTime(dr["ModificationDate"]);
            entity.Link = DBConverter.ToString(dr["Link"]);
            entity.SummaryHtml = DBConverter.ToString(dr["SummaryHtml"]);
            entity.Text = DBConverter.ToString(dr["Text"]);
            entity.Publisher = DBConverter.ToString(dr["Publisher"]);
        }
        public override News GetById(int OId)
        {
            IDataAcessLayerBaseInterface DAL = DALHelper.GetDataAccessLayer();
            DAL.OpenConnection();
            System.Data.IDataReader dr = DAL.ExecuteReader(SP_GetNewsById,
                _OId, OId);
            News entity = new News();
            if (dr.Read())
            {
                TransformToAtomEntity(entity, dr);
            }
            DAL.CloseConnection();
            return entity;
        }

        private List<News> GenerateAtomEntityCollectionFromDataReader(IDataAcessLayerBaseInterface DAL, string storedProcedureName, params object[] StroedProcedureParameters)
        {
            System.Data.IDataReader dr = DAL.ExecuteReader(storedProcedureName, StroedProcedureParameters);
            List<News> activityinfoCollection = new List<News>();
            while (dr.Read())
            {
                News entity = new News(DBConverter.ToInt32(dr["OId"]));
                TransformToAtomEntity(entity, dr);
                activityinfoCollection.Add(entity);
            }
            return activityinfoCollection;
        }

        public List<News> GetTodayNews()
        {
            IDataAcessLayerBaseInterface DAL = DALHelper.GetDataAccessLayer();
            DAL.OpenConnection();
            List<News> activityList =
                GenerateAtomEntityCollectionFromDataReader(
                DAL, SP_GetToday, _Date, DateTime.Now.Date);
            DAL.CloseConnection();
            return activityList;
        }

        /// <summary>
        /// returns certain Day news
        /// </summary>
        /// <param name="date">Date of the day</param>
        /// <returns></returns>
        public override List<News> GetDayNews(DateTime date)
        {
            IDataAcessLayerBaseInterface DAL = DALHelper.GetDataAccessLayer();
            DAL.OpenConnection();
            List<News> activityList =
                GenerateAtomEntityCollectionFromDataReader(
                DAL, SP_GetToday, _Date, date.Date);
            DAL.CloseConnection();
            return activityList;
        }
        /// <summary>
        /// returns certain Day news
        /// </summary>
        /// <param name="fromdate">Date of the day</param>
        /// <returns></returns>
        public override List<News> GetNews(DateTime fromDate, DateTime toDate)
        {
            IDataAcessLayerBaseInterface DAL = DALHelper.GetDataAccessLayer();
            DAL.OpenConnection();
            List<News> activityList =
                GenerateAtomEntityCollectionFromDataReader(
                DAL, SP_GetNewsBetween, _FromDate, fromDate.Date, _ToDate, toDate.Date);
            DAL.CloseConnection();
            return activityList;
        }

        public override List<News> GetAll()
        {
            IDataAcessLayerBaseInterface DAL = DALHelper.GetDataAccessLayer();
            DAL.OpenConnection();
            List<News> activityList =
                GenerateAtomEntityCollectionFromDataReader(
                DAL, SP_GetAllNews, null);
            DAL.CloseConnection();
            return activityList;
        }



        public override void UpdateStatistics()
        {
            throw new NotImplementedException();
        }

        public override List<News> GetDayNews(DateTime dt, int keywordId)
        {
            throw new NotImplementedException();
        }

        public override List<News> GetDayNews(DateTime dt, Guid userId)
        {
            throw new NotImplementedException();
        }
    }
}
